package com.ruoyi.cangku.controller;

import com.ruoyi.cangku.domain.Kucunguanli;
import com.ruoyi.cangku.domain.KucunguanliReturn;
import com.ruoyi.cangku.domain.Shangpinguanli;
import com.ruoyi.cangku.domain.Shangpinguanli2;
import com.ruoyi.cangku.service.IKucunguanliService;
import com.ruoyi.cangku.service.IShangpinguanliService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jinchuguanli.domain.*;
import com.ruoyi.jinchuguanli.service.IGouhuorukuService;
import com.ruoyi.jinchuguanli.service.ITuihuochuliService;
import com.ruoyi.jinchuguanli.service.IXiaoshouchukuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * sahngpinController
 *
 * @author ruoyi
 * @date 2023-11-07
 */
@Controller
@RequestMapping("/cangku/shangpin")
public class ShangpinguanliController extends BaseController {
    private final String prefix = "cangku/shangpin";

    @Autowired
    private IShangpinguanliService shangpinguanliService;

    @Autowired
    private IKucunguanliService kucunguanliService;

    @Autowired
    private IGouhuorukuService gouhuorukuService;

    @Autowired
    private IXiaoshouchukuService xiaoshouchukuService;

    @Autowired
    private ITuihuochuliService tuihuochuliService;

    @Autowired
    private ServerConfig serverConfig;

    @RequiresPermissions("cangku:shangpin:view")
    @GetMapping()
    public String shangping () {
        return prefix + "/shangpin";
    }

    /**
     * 查询sahngpin列表
     */
    @RequiresPermissions("cangku:shangpin:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (Shangpinguanli shangpinguanli) {
        startPage();
        List<Shangpinguanli> list = shangpinguanliService.selectShangpinguanliList(shangpinguanli);
        return getDataTable(list);
    }

    /**
     * 导出sahngpin列表
     */
    @RequiresPermissions("cangku:shangpin:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export (Shangpinguanli shangpinguanli) {
        List<Shangpinguanli> list = shangpinguanliService.selectShangpinguanliList(shangpinguanli);
        ExcelUtil<Shangpinguanli> util = new ExcelUtil<Shangpinguanli>(Shangpinguanli.class);
        return util.exportExcel(list, "sahngpin数据");
    }

    /**
     * 新增sahngpin
     */
    @GetMapping("/add")
    public String add () {
        return prefix + "/add";
    }

    /**
     * 新增保存sahngpin
     */
    @RequiresPermissions("cangku:shangpin:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave (Shangpinguanli2 shangpinguanli) throws IOException {
        String salt = String.valueOf(Math.random());
        String md5log = new Md5Hash(shangpinguanli.getShangpinming() + shangpinguanli.getShangpinleibie()
                + shangpinguanli.getShangpinguigekuan() + shangpinguanli.getShangpinguigegao() + salt).toHex().substring(0, 8);
        shangpinguanli.setShangpinbianhao(md5log);
        String tiaoma = tiaoMaAuto.QRCote(shangpinguanli, serverConfig.getUrl());
        if ("卷闸门".equals(shangpinguanli.getShangpinkaixiang())) {
            shangpinguanli.setShangpinkaixiang(null);
        }
        if (!tiaoma.equals("")) {
            shangpinguanli.setShangpintiaoma(tiaoma);
        }
        if (shangpinguanli.getShuliang() != null) {
            Kucunguanli kucunguanli = new Kucunguanli();
            kucunguanli.setShangpinbianhao(md5log);
            kucunguanli.setShangpintupian(shangpinguanli.getShangpintupian());
            kucunguanli.setShangpintiaoma(shangpinguanli.getShangpintiaoma());
            kucunguanli.setShangpinshuliang(Long.valueOf(shangpinguanli.getShuliang()));
            kucunguanliService.insertKucunguanli(kucunguanli);
        }
        if (shangpinguanli.getShangpintupian().equals("")) {
            shangpinguanli.setShangpintupian(null);
        }
        if (shangpinguanliService.insertShangpinguanli(shangpinguanli) == 0) {
            return toAjax("库存中不允许存在同样的商品");
        }

        return toAjax(1);
    }

    /**
     * 修改sahngpin
     */
    @RequiresPermissions("cangku:shangpin:edit")
    @GetMapping("/edit/{id}")
    public String edit (@PathVariable("id") Integer id, ModelMap mmap) {
        Shangpinguanli shangpinguanli = shangpinguanliService.selectShangpinguanliById(id);
        mmap.put("shangpinguanli", shangpinguanli);
        return prefix + "/edit";
    }

    /**
     * 修改保存sahngpin
     */
    @RequiresPermissions("cangku:shangpin:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave (Shangpinguanli shangpinguanli) throws IOException {

        //String bianhaoOld = shangpinguanli.getShangpinbianhao();
        //shangpinguanli.setShangpinbianhao(null);

        Shangpinguanli shangpinguanli1 = shangpinguanliService.selectShangpinguanliById(shangpinguanli.getId());

        if (!shangpinguanli1.getShangpinleibie().equals(shangpinguanli.getShangpinleibie())
                || !shangpinguanli1.getShangpinming().equals(shangpinguanli.getShangpinming())
                || !shangpinguanli1.getShangpinguigekuan().equals(shangpinguanli.getShangpinguigekuan())
                || !shangpinguanli1.getShangpinguigegao().equals(shangpinguanli.getShangpinguigegao())
                || !shangpinguanli1.getShangpinkaixiang().equals(shangpinguanli.getShangpinkaixiang())) {

            String tiaoma = tiaoMaAuto.QRCote(shangpinguanli, serverConfig.getUrl());

            if (!tiaoma.equals("")) {
                shangpinguanli.setShangpintiaoma(tiaoma);
            }

            List<GouhuorukuReturn> gouhuorukuReturns = gouhuorukuService.selectGouhuorukuReturnList(new Gouhuoruku());

            for (GouhuorukuReturn gr : gouhuorukuReturns) {
                for (Shangpin s : gr.getShangpin()) {
                    if (Objects.equals(s.getShangpinbianhao(), shangpinguanli.getShangpinbianhao())) {
                        BeanUtils.copyProperties(s, shangpinguanli);
                    }
                }
                gouhuorukuService.updateGouhuorukuReturn(gr);
            }

            List<XiaoshouchukuReturn> xiaoshouchukus = xiaoshouchukuService.selectXiaoshouchukuReturnList(new Xiaoshouchuku());
            for (XiaoshouchukuReturn xr : xiaoshouchukus) {
                for (Shangpin s : xr.getShangpin()) {
                    if (Objects.equals(s.getShangpinbianhao(), shangpinguanli.getShangpinbianhao())) {
                        BeanUtils.copyProperties(s, shangpinguanli);
                    }
                }
                xiaoshouchukuService.updateXiaoshouchukuReturn(xr);
            }

            List<TuihuochuliReturn> tuihuochuliReturns = tuihuochuliService.selectTuihuochuliReturnList(new Tuihuochuli());

            for (TuihuochuliReturn tr : tuihuochuliReturns) {
                for (Shangpin s : tr.getShangpin()) {
                    if (Objects.equals(s.getShangpinbianhao(), shangpinguanli.getShangpinbianhao())) {
                        BeanUtils.copyProperties(s, shangpinguanli);
                    }
                }
                tuihuochuliService.updateTuihuochuliReturn(tr);
            }
        }
        return toAjax(shangpinguanliService.updateShangpinguanli(shangpinguanli));
    }

    /**
     * 删除sahngpin
     */
    @RequiresPermissions("cangku:shangpin:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove (String ids) {
        String[] idlist = Convert.toStrArray(ids);
        for (String id:idlist
             ) {
            Shangpinguanli shangpinguanli = shangpinguanliService.selectShangpinguanliById(Integer.parseInt(id));
            Kucunguanli kucunguanli = new Kucunguanli();
            kucunguanli.setShangpinbianhao(shangpinguanli.getShangpinbianhao());
            List<Kucunguanli> kucunguanliList = kucunguanliService.selectKucunguanliList(kucunguanli);
            for (Kucunguanli k:kucunguanliList
                 ) {
                kucunguanliService.deleteKucunguanliById(k.getId());
            }
        }
        return toAjax(shangpinguanliService.deleteShangpinguanliByIds(ids));
    }
}
